package com.sensePlusVR.widget.dialog;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;

import com.sensePlusVR.main.R;

/**
 * 
 * @author: Loren
 * @create: 2016-8-10下午3:08:42
 * @desc: 蓝牙连接悬浮全屏对话框
 *
 */
@SuppressLint("HandlerLeak")
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
@SuppressWarnings("unused")
public class BlueOpenBlueDialog extends BaseDialog implements OnClickListener{
	private static final String TAG = BlueOpenBlueDialog.class.getSimpleName();
	private Context context;
	private LinearLayout dialog_top_linear;
	private Button blue_connect_btn;
	
	private BluetoothManager mBluetoothManager;
	private BluetoothAdapter mBluetoothAdapter;
	
	private static final int MSG_START_SCAN = 1;
	
	private BlueEnable blueEnable;
	
	public interface BlueEnable{
		public void onBlueEnable();
		public void onBandDismiss();
	}
	
	public void setOnBlueEnable(BlueEnable blueEnable){
		this.blueEnable = blueEnable;
	}
	
	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case MSG_START_SCAN:
				if(!mBluetoothAdapter.isEnabled()){
					mHandler.sendEmptyMessageDelayed(MSG_START_SCAN, 1000);
				}else{
					blueEnable.onBlueEnable();
				}
				break;

			default:
				break;
			}
		};
	};
	
	public BlueOpenBlueDialog(Context context) {
		super(context);
		this.context = context;
		initData();
		
	}

	public BlueOpenBlueDialog(Context context, int theme) {
		super(context, theme);
		this.context = context;
		initData();
	}
	
	private void initData() {
		mBluetoothManager = (BluetoothManager) context.getSystemService(Context.BLUETOOTH_SERVICE);
		mBluetoothAdapter = mBluetoothManager.getAdapter();
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.dialog_blue_connect_over);
		
		blue_connect_btn = (Button) findViewById(R.id.blue_connect_btn);
		dialog_top_linear = (LinearLayout) findViewById(R.id.dialog_top_linear);
		blue_connect_btn.setOnClickListener(this);
		dialog_top_linear.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.dialog_top_linear:
			blueEnable.onBandDismiss();
			break;
		case R.id.blue_connect_btn:
			//MyToast.showMessage(context, R.string.blue_reconnect);
			 if (!mBluetoothAdapter.isEnabled()) {
				 mBluetoothAdapter.enable();
				 mHandler.sendEmptyMessageDelayed(MSG_START_SCAN, 1000);
			 }
			break;
		}
		if(isShowing())dismiss();
	}
}
